package com.koron.order.defect.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.common.bean.po.OrderDispatch;
import com.koron.order.common.bean.po.OrderDispatchWorker;
import com.koron.order.common.bean.po.OrderExecute;
import com.koron.order.common.bean.po.OrderExecutePart;
import com.koron.order.common.service.OrderDispatchService;
import com.koron.order.common.service.OrderDispatchWorkerService;
import com.koron.order.common.service.OrderExecutePartService;
import com.koron.order.common.service.OrderExecuteService;
import com.koron.order.defect.bean.convertor.DefectConvertor;
import com.koron.order.defect.bean.po.DefectExecute;
import com.koron.order.defect.bean.po.DefectStatus;
import com.koron.order.defect.bean.vo.DefectExecuteVO;
import com.koron.order.defect.mapper.ExecuteMapper;
import com.koron.order.defect.service.DefectExecuteService;
import com.koron.order.defect.service.DefectService;
import com.koron.order.defect.service.ExecuteService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName DefectOrderExecuteServiceImpl
 * @description
 * @Author zhouj
 * @Date 2021/9/6 16:34
 */
@Service("defectOrderExecuteService")
public class ExecuteServiceImpl implements ExecuteService {

    @Autowired
    private DefectExecuteService defectExecuteService;

    @Autowired
    private OrderExecuteService orderExecuteService;

    @Autowired
    private OrderExecutePartService orderExecutePartService;

    @Autowired
    private DefectService defectService;

    @Autowired
    private DefectConvertor defectConvertor;

    @Autowired
    private OrderDispatchService orderDispatchService;

    @Autowired
    private OrderDispatchWorkerService orderDispatchWorkerService;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public String insertOrUpdate(SessionFactory factory, DefectExecuteVO defectExecuteVO) {

        //入库前可根据工单执行id对  工单执行表:新增或更新;工单执行备件信息表:批量删除再批量新增; 缺陷执行表:新增或更新

        //工单执行信息
        OrderExecute orderExecute = defectConvertor.voToOrderExecuteBean(defectExecuteVO);
        String executeId = orderExecute.getId();
        String orderId = orderExecute.getOrderId();
        String orderType = defectExecuteVO.getOrderType();
        if (StringUtils.isEmpty(executeId)) {
            executeId = CodeTools.getCode32();
            orderExecute.setId(executeId);
        }
        orderExecuteService.insertOrUpdate(factory, orderExecute);

        //缺陷执行信息
        DefectExecute defectExecute = defectConvertor.voToDefectExecuteBean(defectExecuteVO);
        defectExecute.setDefectId(orderId);
        defectExecute.setExecuteId(executeId);

        defectExecuteService.insertOrUpdate(factory, defectExecute);

        //工单执行备件列表信息
        List<OrderExecutePart> orderExecutePart = defectExecuteVO.getOrderExecutePart();

        //工单备件执行信息
        if (!CollectionUtils.isEmpty(orderExecutePart)) {
            String finalExecuteId = executeId;
            orderExecutePart.forEach(e -> {
                e.setExecuteId(finalExecuteId);
                e.setOrderId(orderId);
                e.setOrderType(orderType);
            });
        }

        orderExecutePartService.insertBatch(factory, orderExecutePart);

        //是否提交
        if (defectExecuteVO.getIsCommit()) {
            defectService.submitDefect(factory, orderId, DefectStatus.WAIT_ACCEPT.getStatus());
        }

        return executeId;
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<DefectExecuteVO> defectExecuteVOList, Boolean isCommit) {

        //获取第一条数据工单id
        String batchOrderId = defectExecuteVOList.get(0).getOrderId();

        defectExecuteVOList.forEach(defectExecuteVO -> {
            OrderExecute orderExecute = defectConvertor.voToOrderExecuteBean(defectExecuteVO);
            String executeId = orderExecute.getId();
            String orderId = orderExecute.getOrderId();
            if (StringUtils.isEmpty(executeId)) {
                executeId = CodeTools.getCode32();
            }
            //数据可能为新建或者修改,所以需要使用 insertOrUpdate,以下都是
            orderExecuteService.insertOrUpdate(factory, orderExecute);

            //缺陷执行信息
            DefectExecute defectExecute = defectConvertor.voToDefectExecuteBean(defectExecuteVO);
            defectExecute.setDefectId(orderId);
            defectExecute.setExecuteId(executeId);

            defectExecuteService.insertOrUpdate(factory, defectExecute);

            //工单执行备件列表信息
            List<OrderExecutePart> orderExecutePart = defectExecuteVO.getOrderExecutePart();

            //工单备件执行信息
            if (!CollectionUtils.isEmpty(orderExecutePart)) {
                String finalExecuteId = executeId;
                orderExecutePart.forEach(e -> {
                    e.setExecuteId(finalExecuteId);
                    e.setOrderId(orderId);
                });
            }
            //直接对配件信息进行先删除
            orderExecutePartService.insertBatch(factory, orderExecutePart);

        });

        if (isCommit) {
            defectService.submitDefect(factory, batchOrderId, DefectStatus.WAIT_ACCEPT.getStatus());
        }

        return 1;
    }

    @Override
    @TaskAnnotation("deleteByOrderExecuteId")
    public int deleteByOrderExecuteId(SessionFactory factory, String executeId) {
        //工单id删除执行信息,需要根据工单执行id 删除工单执行,缺陷执行,工单执行配件
        orderExecuteService.deleteById(factory, executeId);
        defectExecuteService.deleteByOrderExecuteId(factory, executeId);
        orderExecutePartService.deleteByOrderExecuteId(factory, executeId);
        return 1;
    }


    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        //工单id删除执行信息,需要根据工单id 删除工单执行,缺陷执行,工单执行配件
        orderExecuteService.deleteByOrderId(factory, orderId);
        defectExecuteService.deleteByOrderId(factory, orderId);
        orderExecutePartService.deleteByOrderId(factory, orderId);
        return 1;
    }

    //工单id查询执行列表
    @Override
    @TaskAnnotation("queryListByOrderId")
    public List<DefectExecuteVO> queryListByOrderId(SessionFactory factory, String orderId) {

        List<DefectExecuteVO> defectExecuteVOList = factory.getMapper(ExecuteMapper.class).queryByOrderId(orderId);

        return defectExecuteVOList;
    }

    //工单执行id查询vo
    @Override
    @TaskAnnotation("queryListByOrderExecuteId")
    public DefectExecuteVO queryListByOrderExecuteId(SessionFactory factory, String executeId) {

        DefectExecuteVO defectExecuteVO = factory.getMapper(ExecuteMapper.class).queryListByOrderExecuteId(executeId);

        return defectExecuteVO;
    }

    @Override
    @TaskAnnotation("querySubmitUserListByOrderId")
    public List<String> querySubmitUserListByOrderId(SessionFactory factory, String orderId) {
        //根据工单id查询提交的人员  派工处设置好的

        OrderDispatch orderDispatch = orderDispatchService.queryByOrderId(factory, orderId);

        List<OrderDispatchWorker> orderDispatchWorkers = orderDispatchWorkerService.queryByOrderId(factory, orderId);

        List<String> list = new ArrayList<>();

        if (!Objects.isNull(orderDispatch)) {
            if (orderDispatch.getIsOut() == 1) {
                list.add(orderDispatch.getOutLeader());
            } else {
                list.add(orderDispatch.getLeader());
            }
        }

        if (!CollectionUtils.isEmpty(orderDispatchWorkers)) {
            orderDispatchWorkers.forEach(p -> {
                list.add(p.getWorker());
            });
        }

        return list;
    }

}
